package com.ruyuan.eshop.diplomacy.manager.custom.db;

import com.google.common.collect.Sets;
import com.ruyuan.eshop.common.enums.DelFlagEnum;
import com.ruyuan.eshop.diplomacy.domain.entity.FrontCategoryRelationDO;
import com.ruyuan.eshop.diplomacy.manager.common.cache.RedisStringCache;
import com.ruyuan.eshop.diplomacy.manager.common.db.AbstractRedisStringDatabase;
import com.ruyuan.eshop.diplomacy.mapper.FrontCategoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * 前台类目关联商品信息
 *
 * @author zhonghuashishan
 */
@Slf4j
@Service("frontCategoryRelationStringDatabase")
public class FrontCategoryRelationStringDatabase extends AbstractRedisStringDatabase<FrontCategoryRelationDO> {
    @Resource
    private FrontCategoryMapper frontCategoryMapper;

    /**
     * 表名
     */
    private static final String TABLE_NAME = "FRONT_CATEGORY_RELATION";

    /**
     * 前端类目ID
     */
    public static final String FRONT_CATEGORY_ID = "frontCategoryId";
    /**
     * 数据源单表
     *
     * @return java.lang.String
     * @author zhonghuashishan
     */
    @Override
    public String getTableName() {
        return TABLE_NAME;
    }

    /**
     * 数据源全量表
     *
     * @return java.util.Set<java.lang.String>
     * @author zhonghuashishan
     */
    @Override
    public Set<String> getTableNameSet() {
        return Sets.newHashSet(TABLE_NAME);
    }


    /**
     * 获取表数据
     *
     * @param tableFieldsMap    key 数据库字段，value 字段值
     * @param queryType         查询类型，多数据查询时区分
     * @return java.util.Optional<com.ruyuan.eshop.diplomacy.domain.entity.FrontCategoryDO>
     * @author zhonghuashishan
     */
    @Override
    public Optional<FrontCategoryRelationDO> getTableData(Map<String, Object> tableFieldsMap, String queryType) {
        if (tableFieldsMap.containsKey(FRONT_CATEGORY_ID)) {

            List<FrontCategoryRelationDO> frontCategoryDOList = frontCategoryMapper.queryFrontCategoryList(Arrays.asList(Long.valueOf(tableFieldsMap.get(FRONT_CATEGORY_ID).toString())));

            if (!CollectionUtils.isEmpty(frontCategoryDOList)) {
                FrontCategoryRelationDO doBase = frontCategoryDOList.get(0);
                if (Objects.equals(DelFlagEnum.EFFECTIVE.getCode(), doBase.getDelFlag())) {
                    return Optional.of(doBase);
                }
            }
            return Optional.empty();
        }
        throw new UnsupportedOperationException();
    }


    @Override
    public Optional<List<FrontCategoryRelationDO>> listTableData(Map<String, Object> tableFieldsMap, String queryType) {
        return Optional.empty();
    }

    @Override
    public Optional<RedisStringCache<FrontCategoryRelationDO>> listTableDataByIdSet(Set<Integer> idSet, String queryType) {
        return Optional.empty();
    }

    /**
     * 根据前台类目批量查询下属商品
     * @param idSet         表主键ID集合
     * @param queryType     查询类型，多数据查询时区分
     * @return
     */
    @Override
    public Optional<RedisStringCache<FrontCategoryRelationDO>> listTableDataByIdSet(List<Long> idSet, String queryType) {
        List<FrontCategoryRelationDO> frontCategoryRelationDOS = frontCategoryMapper.queryFrontCategoryList(idSet);
        if (CollectionUtils.isEmpty(frontCategoryRelationDOS)){
            RedisStringCache redisStringCache = new RedisStringCache();
            List<FrontCategoryRelationDO> addList = frontCategoryRelationDOS.stream().filter(doBase -> doBase.getDelFlag().equals(DelFlagEnum.EFFECTIVE.getCode())).collect(toList());
            Set<String> deleteSet = frontCategoryRelationDOS.stream().filter(doBase -> doBase.getDelFlag().equals(DelFlagEnum.DISABLED.getCode())).map(doBase -> String.valueOf(doBase.getId())).collect(toSet());
            redisStringCache.setAddList(addList);
            redisStringCache.setDeleteSet(deleteSet);
            return Optional.of(redisStringCache);
        }
        return Optional.empty();
    }


}
